import { base_url } from './config.js';

export function getActivity(id) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: base_url + '/activity/activity?id=' + id,
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Authorization': uni.getStorageSync('token'),
      },
      success(res) {
        console.log('【 Response in request getActivity 】', res);
        resolve(res.data);
      },
      fail(err) {
        console.log('【 Error in request getActivity 】', err);
        reject({ success: false });
      }
    });
  });
}

export function getActivityList(cursor, limit) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: base_url + `/activity/activity-list?limit=${limit}${cursor ? '&cursor=' + cursor : ''}`,
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Authorization': uni.getStorageSync('token'),
      },
      success(res) {
        console.log('【 Response in request getActivityList 】', res);
        resolve(res.data);
      },
      fail(err) {
        console.log('【 Error in request getActivityList 】', err);
        reject({ success: false });
      }
    });
  });
}

export function participateActivity(activityId) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: base_url + `/activity/join-activity`,
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'Authorization': uni.getStorageSync('token'),
      },
      data: {
        activityId: activityId
      },
      success(res) {
        console.log('【 Response in request participateActivity 】', res);
        resolve(res.data);
      },
      fail(err) {
        console.log('【 Error in request participateActivity 】', err);
        reject({ success: false });
      }
    });
  });
}

export function cancelActivityParticipation(activityId) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: base_url + `/activity/cancel-activity`,
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'Authorization': uni.getStorageSync('token'),
      },
      data: {
        activityId: activityId
      },
      success(res) {
        console.log('【 Response in request cancelActivityParticipation 】', res);
        resolve(res.data);
      },
      fail(err) {
        console.log('【 Error in request cancelActivityParticipation 】', err);
        reject({ success: false });
      }
    });
  });
}